package com.hp.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.hp.ssm.entity.User;
import com.hp.ssm.service.UserService;
import com.hp.ssm.util.GeneratID;
import com.hp.ssm.util.ResultJson;
import com.hp.ssm.util.Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author 23129
 * 员工
 */
@Controller()
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/findAll")
    @ResponseBody
    public void findAll(User user, Integer page, Integer limit,HttpServletResponse resp) {
        System.out.println(page + "  " + limit+"  "+user);
        List<User> list = userService.findAll(user,page,limit);
        PageInfo<User> userPageInfo = new PageInfo<User>(list);
        System.out.println(list);
        System.out.println(list.size());
        Utils.returnJson(ResultJson.success(userPageInfo), resp);
    }

    @RequestMapping("/delfindAll")
    @ResponseBody
    public void delfindAll(User user, Integer page, Integer limit,HttpServletResponse resp) {
        List<User> list = userService.delfindAll(user, page,limit);
        PageInfo<User> userPageInfo = new PageInfo<User>(list);
        Utils.returnJson(ResultJson.success(userPageInfo), resp);
    }

    @RequestMapping("/add")
    @ResponseBody
    public void add(HttpServletResponse resp, User user) {
        System.out.println(user);
         Date  atime = new Date();
         user.setAtime(atime);
          String apuser = GeneratID.getGeneratID();
          user.setApuser(apuser);
        int i = userService.add(user);
        Utils.returnJson(ResultJson.success(i), resp);
    }

    @RequestMapping("/showById")
    @ResponseBody
    public void showById(HttpServletResponse resp, Integer aid) {
        System.out.println(aid);
        User user = userService.showById(aid);
        Utils.returnJson(ResultJson.success(user), resp);
    }

    @RequestMapping("/update")
    @ResponseBody
    public void update(HttpServletResponse resp, User user) {
        System.out.println(user);
        int i = userService.update(user);
        Utils.returnJson(ResultJson.success(i), resp);
    }

    @RequestMapping("/del")
    @ResponseBody
    public void del(HttpServletResponse resp, Integer aid) {
        System.out.println(aid);
        int i = userService.del(aid);
        Utils.returnJson(ResultJson.success(i), resp);
    }
    @RequestMapping("/recover")
    @ResponseBody
    public void recover(HttpServletResponse resp, Integer aid) {
        System.out.println(aid);
        int i = userService.recover(aid);
        Utils.returnJson(ResultJson.success(i), resp);
    }
}
